﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem044
    {
        /// <summary>
        /// Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
        /// </summary>
        [EulerProblem(44, Title = "Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.")]
        public static long Solve()
        {
            Func<long, bool> isPentagonal = x => (Math.Sqrt(24 * x + 1) + 1) % 6 == 0;
            for (int j = 1; j < 10000; j++)
            {
                Console.Title = j.ToString();
                for (int k = j; k < 10000; k++)
                {
                    long pk = GetPentagonal(k);
                    long pj = GetPentagonal(j);
                    if (isPentagonal(pk + pj) && isPentagonal(pk - pj))
                    {
                        return pk - pj;
                    }
                }
            }

            return 0;
        }

        static long GetPentagonal(this int n)
        {
            return n * (3 * n - 1) / 2;
        }
    }
}
